//
//  OriginalView.swift
//  WeiBo test
//
//  Created by Aquarius_wing on 16/5/9.
//  Copyright © 2016年 Wing. All rights reserved.
//

import UIKit
import SnapKit
class OriginalView: UIView {
    let iconWidth:CGFloat=50
    lazy var iconImage=UIImageView()
    lazy var nameLabel=UILabel(title: "", size: 14, color: .blackColor())
    lazy var vipImage=UIImageView()
    lazy var timeLabel=UILabel(title: "11.11", size: 14, color: .lightGrayColor())
    lazy var sourcelabel=UILabel(title: "", size: 14, color: .lightGrayColor())
    lazy var contentLabel=UILabel(title: "", size: 14, color: .darkGrayColor(),margin: cellMargin)
    lazy var contentImage=PictureView()//这个时候它的image为nil
    //原创微博底部约束  用来记录底部约束  后续根据是否有配图 来动态修改
    var bottomConstraints: Constraint?
    var weiboContent:WeiboContent?{
        didSet{
            
            
            //初始化iconImage
            iconImage.sd_setImageWithURL(weiboContent?.headImageURL, placeholderImage: UIImage(named: "avatar_default_big"))
            //初始化nameLabel
            nameLabel.text=weiboContent?.name
            //初始化vipImage
//            if weiboContent?.vip==1{
//                //是VIP
//                vipImage.image=UIImage(named: "vip")
//                vipImage.hidden=false
//            }else{
//                //不是VIP
//                vipImage.hidden=true
//            }
            //初始化timeLable
            //取出当前时间
            let timeNow=weiboContent?.time
            //初始化一个时间格式
            let yearFm=NSDateFormatter()
            
            //yyyy-MM-dd HH:mm:ss 时间格式
            yearFm.dateFormat="yyyy-MM-dd HH:mm:ss"
//            let yearT=yearFm.stringFromDate(timeNow!)
            print("时间",timeNow!,yearFm)
            timeLabel.text=weiboContent?.name
            //sourcelabel
            let sourceText=String(format: "%@",(weiboContent?.source)!)
            sourcelabel.text="来自"+sourceText.html2String
            //初始化contentLabel
            contentLabel.text=weiboContent?.text
            
            bottomConstraints?.uninstall()
            //初始化contentImage
            if weiboContent?.pic_urls?.count != 0 {
                //当有图片的时候
                contentImage.picURLs=(weiboContent?.imageURLs)!
                contentImage.hidden=false
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    //将约束条件转换成 约束类型
                    bottomConstraints = make.bottom.equalTo(contentImage.snp_bottom).offset(cellMargin).constraint
                })
            }else{
                //当没有图片的时候
                contentImage.hidden=true
                //更新约束
                self.snp_updateConstraints(closure: { (make) in
                    bottomConstraints=make.bottom.equalTo(contentLabel.snp_bottom).offset(cellMargin).constraint
                })
            }
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setupUI(){
        //添加子视图
        addSubview(iconImage)
        addSubview(nameLabel)
//        addSubview(vipImage)
        addSubview(timeLabel)
        addSubview(sourcelabel)
        addSubview(contentLabel)
        addSubview(contentImage)
        
        //添加约束信息
        iconImage.snp_makeConstraints { (make) in
            make.top.equalTo(self.snp_top).offset(cellMargin)
            make.left.equalTo(self.snp_left).offset(cellMargin)
            make.height.equalTo(iconWidth)
            make.width.equalTo(iconWidth)
        }
        nameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(iconImage.snp_top)
            make.left.equalTo(iconImage.snp_right).offset(cellMargin)
        }
//        vipImage.snp_makeConstraints { (make) in
//            make.top.equalTo(nameLabel.snp_top)
//            make.left.equalTo(nameLabel.snp_right).offset(cellMargin)
//        }
        timeLabel.snp_makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.bottom.equalTo(iconImage)
        }
        sourcelabel.snp_makeConstraints { (make) in
            make.left.equalTo(timeLabel.snp_right).offset(cellMargin)
            make.bottom.equalTo(timeLabel)
        }
        contentLabel.snp_makeConstraints { (make) in
            make.top.equalTo(iconImage.snp_bottom).offset(cellMargin)
            make.left.equalTo(iconImage.snp_left)
            make.right.equalTo(self.snp_right).offset(-cellMargin)
        }
        //设置约束
        contentImage.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(cellMargin)
            make.left.equalTo(contentLabel.snp_left)
//            if (contentImage.picName != nil) {
//                //如果有图片
//                //                let image=contentImage.image
//                //                let width=SCREENSIZE.width*0.8
//                //                let height=(image?.size.height)!/(image?.size.width)!*width
//            }else{
//            }
        }
        self.snp_updateConstraints(closure: { (make) -> Void in
            //将约束条件转换成 约束类型
            bottomConstraints = make.bottom.equalTo(contentImage.snp_bottom).offset(cellMargin).constraint
        })
        
    }

}
